<?php
namespace Mapi\Controller;
use Mapi\Controller\BaseController;

class UserController extends BaseController {    

    /**
     * 在已登录的情况下修改密码
     * @return [type] [description]
     */
    public function changePwdByLogin() {
        apigetpost();
        $password = I('post.password');         
        if(!$password) {
            $message['resultCode'] = 1;
            $message['message'] = '密码不能为空';           
            $this->ajaxReturn($message);             
        }

        $dataGet['uid'] = $this->uid;
        $dataGet['password'] = md5(C('PWD_SALT') . $password);
        $res = M('User')->save($dataGet);
        if($res !== false) {
            $message['resultCode'] = 0;
            $message['message'] = '修改成功';           
            $this->ajaxReturn($message);          
        } else {
            $message['resultCode'] = 1;
            $message['message'] = '密码修改失败，请重试';           
            $this->ajaxReturn($message);
        }
    }


    /**
     * 获取用户列表
     * @return [type] [description]
     */
    public function getReportList() {
        $fullName = I('get.fullName');
        $uModel = D('UserInfo');
        if($fullName) {
            $fullName=strtolower(zh2py($fullName));
            
            // $map['realname'] = array('like', '%'.$fullName . '%');
            $map['realnamepy'] = array('like', '%'.$fullName . '%');
            //$map='MATCH(realnamepy) AGAINST("'.$fullName.'")';
            // $info = $uModel->field('uid, realname,ch.name channel,j.name position')->where($map)
            // ->alias('u')
            // ->join('__CHANNEL__ ch ON ch.chid=u.userchannel ')
            // ->join('__JOB__ j ON j.jid=u.userjobs')
            // ->select();
            $info = $uModel->field('uid, realname, userchannel, userjobs')->where($map)->select();

            foreach($info as $key => $val) {
                $info[$key]['channel'] = M('Channel')->where(array('chid' => $val['userchannel']))->getField('name');
                $info[$key]['position'] = M('Job')->where(array('jid' => $val['userjobs']))->getField('name');
                unset($info[$key]['userchannel']);
                unset($info[$key]['userjobs']);
            }
            
            if($info) {
                $message['resultCode'] = 0;
                $message['message'] = '';       
                $message['userList'] = $info;    
                $this->ajaxReturn($message);                 
            } else {
                $message['resultCode'] = 0;
                $message['message'] = '用户不存在'; 
                $message['userList'] = array();          
                $this->ajaxReturn($message);                
            }
        } else {
            $message['resultCode'] = 1;
            $message['message'] = '请输入用户名';           
            $this->ajaxReturn($message);
        }
    }

    public function getUserInfo() {
        $userinfo = M('UserInfo')->where(array('uid' => $this->uid))->find();
        if($userinfo) {
            $arr['resultCode']=0;
            $arr['message']='';
            $arr=array_merge($arr,array(
                "userId"=>$userinfo['uid'],
                "fullName"=>$userinfo['realname']?$userinfo['realname']:'',
                "mobile"=>$userinfo['mobile']?$userinfo['mobile']:'',
                "mediaName"=>$userinfo['workunit']?$userinfo['workunit']:'',
                "status"=>$userinfo['status']?$userinfo['status']:'',
                'channelName' => M('Channel')->where(array('chid' => $userinfo['userchannel']))->getField('name'),
                'positionName' => M('Job')->where(array('jid' => $userinfo['userjobs']))->getField('name'),
            ));
        } else {
            $arr=array(
                'resultCode'=>1,
                'message'=>'用户未登录',
            );          
        }

        $this->ajaxReturn($arr);
    }
}